/*
 * @Author: 一品网络技术有限公司
 * @Date: 2022-06-22 10:40:08
 * @LastEditTime: 2022-10-06 15:15:54
 * @FilePath: \ypcmsvue3\src\router\index.js
 * @Description:
 * 联系QQ:58055648
 * Copyright (c) 2022 by 东海县一品网络技术有限公司, All Rights Reserved.
 */
import store from "@/store";
import { createRouter, createWebHistory } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import ModelIndex from "./../pages/model/index.vue";
const modules = import.meta.glob("./../pages/**/*.vue");

/*进度条*/
NProgress.configure({
    showSpinner: true,
});
const routes = [
    {
        path: "/",
        name: "Home",
        component: () => import("@/pages/home"),
        redirect: "/welcome",
        meta: {
            title: "控制台",
            icon: "ri-spectrum-fill",
        },
        children: [
            {
                path: "/welcome",
                component: () => import("../pages/welcome.vue"),
                meta: {
                    title: "欢迎页面",
                    icon: "",
                },
            },
            {
                path: "/my",
                component: () => import("../pages/permissions/admin/my.vue"),
                meta: {
                    title: "个人中心",
                },
            },
            {
                path: "/model/:modname",
                name: "ModCon",
                component: ModelIndex,
                meta: {
                    title: "数据模型",
                    icon: "",
                },
            }
        ],
    },
    {
        path: "/login.html",
        name: "Login",
        component: () => import("@/pages/login"),
        hidden: true,
        meta: {
            title: "登录",
            icon: "ri-login-circle-line",
        },
    },
    {
        path: "/loginOut",
        name: "loginOut",
        component: () => import("@/pages/login.vue"),
        meta: {
            title: "安全退出",
            icon: "ri-login-circle-line",
        },
    },
];
const router = createRouter({
    history: createWebHistory(),
    base:'/',
    routes: routes,
});

//增加动态路由
let addluyou;
const ypAddRoute = (route) => {
    let component =null
    let redirect=null
    if(route.type==2){
        component = modules[`../pages${route.path}/index.vue`] || null;

        
        if(route.children){

            for(let i in route.children){
                if(route.children[i].path.indexOf('/list')>0){
                    redirect=route.path+'/list'
                }
            }
        }

  
    }
    if(route.type==3){
        component = modules[`../pages${route.path}.vue`] || null;
    }
    let children = [];
    if (route.children && route.children.length) {
        route.children.forEach((item) => {
            if (item.path.indexOf("Listinfo") >= 0) {
                return;
            }
            if (item.path.indexOf("model") >= 0) {
                return;
            }
            children.push(ypAddRoute(item));
        });
    }



    addluyou = {
        path: route.path,
        name: route.name,
        meta: route.meta||{title:route.title,icon:route.icon},
        redirect:redirect,
        component: component,
        children: children,
    };
    return addluyou;
};
const loadAsyncRoutes = () => {
    var loadRoute = JSON.parse(sessionStorage.getItem("route"))||[]
    loadRoute.forEach((item) => {
        addluyou = ypAddRoute(item);
        router.addRoute("Home", addluyou)
    });
};
loadAsyncRoutes();
router.beforeEach(async (to, from, next) => {
    NProgress.start();

    let token = store.state.token;
    if (token) {
        await store.dispatch("GET_BASE",to);
    }
    if (!token && to.name != "Login") {
        next({ name: "Login" });
    } else if (to.name == "Login" && token) {
        next({ name: "Home" });
    } else {
        if(to.params.modname){
            to.matched.forEach((v, k) => {
                if (v.name == "ModCon") {
                    to.matched[k].meta.title = store.state.modInfo.alias;
                    to.matched[k].meta.icon = store.state.modInfo.icon;
                }
            });

        }
        next();
    }
});
router.afterEach(() => {
    NProgress.done();
});

export { router, routes,loadAsyncRoutes };
